/**
 * File: Profile.java
 * Project: CMSC 345 - VirtualAdvisor
 * @author Justin Alt
 * Date: April 18, 2011
 * E-mail: justalt1@umbc.edu 
 * Description: 
 */

package virtualadvisor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Profile {
	static private final String profileFile = "Profile.xml";
	static private final String cmscCatalog = "cmscCatalog.xml";
	static private final String isCatalog = "isCatalog.xml";
	static private final String gepCatalog = "gepCatalog.xml";
	static private final boolean DEBUG = false;
	private Student student;
	private String currentPlan;
	private DegreeProgress progress;

	public static void main(String[] args) {
		/*List<String> myMajor = new LinkedList<String>();
		myMajor.add("Computer Science Major (CMSC)");
		Student s = new Student("Justin Alt", myMajor, "Business Minor");
		
		/*PlanMy plan = new PlanMy();
		
		LinkedList<CourseMy> myPre = new LinkedList<CourseMy>();
		DegreeProgressMy dp = new DegreeProgressMy();
		CourseMy c0 = new CourseMy("MATH", 150, "Math COURSE", null, null, 4);
		myPre.add(c0);
		
		CourseMy cmsc201 = new CourseMy("CMSC", 201, "Computer Science I for Majors", null, null, 4);
		List<CourseMy> myCourseList = new LinkedList<CourseMy>();
		myCourseList.add(cmsc201);
		dp.addCompletedCourses(cmsc201);
		SemesterMy sem = new SemesterMy("Spring", 2010, myCourseList, dp);
		plan.addSemester(sem);
		
		ProfileMy p = new ProfileMy(s, plan, dp); 
		//System.out.println(p.XMLCoder());
		//p.saveProfileXML("t.xml");
		//p.loadProfileXMLFile("test.xml", "CMSC_IS");
		//System.out.println(p.profileFileNamer());
		
		// Testing getAllPlanNames
		String[] x = p.getAllPlanNames("test.xml");
		for(String y: x){
			System.out.println((String)y);
		}
		*/

		Profile p = new Profile();
		//System.out.println(p.toString());
	p.loadPlanXMLFile("CMSC_IS_BUSINESS", cmscCatalog);
		//p.saveProfileXML();
		//System.out.println(p.findCourseInfoXML("courseCatalog.xml", "CMSC201"));
	}
	
	/**
	 * Name: Default Constructor
	 */
	public Profile(){
		this.student = new Student();
		this.progress = null;
	}
	
	/**
	 * Name: Constructor
	 * Precondition: Needs a student object.
	 * Postcondition: Creates a new profile.
	 * @param student - Student object.
	 */
	public Profile(Student student){
		this.student = student;
		
		LinkedList<String> gep = getAllGEPCourses(gepCatalog);
		
		LinkedList<String> majors = (LinkedList<String>)student.getMajor();
		if(majors.size() == 2)
		{
			LinkedList<Course> req = getAllMajorCourses(cmscCatalog);
			req.addAll(getAllMajorCourses(isCatalog));
			progress = new DegreeProgress(req, gep);
		}
		else
		{
			String m = majors.get(0);
			if(m.equals("Computer Science Major (CMSC)")){
				LinkedList<Course> req = getAllMajorCourses(cmscCatalog);
				progress = new DegreeProgress(req, gep);
			} else {
				LinkedList<Course> req = getAllMajorCourses(isCatalog);
				progress = new DegreeProgress(req, gep);
			}
		}
	}
	
	/**
	 * Name: Constructor
	 * Precondition: Needs a Student and a DegreeProgress object.
	 * Postcondition: Creates a new profile.
	 * @param student - Student object.
	 * @param progress - DegreeProgress object.
	 */
	public Profile(Student student, String profileName){
		this(student);
		this.student = student;
		if(fileExists(profileFile)){
			if(student.getMajor().equals("CMSC")){
				//LinkedList<Course> req = getAllMajorCourses(cmscCatalog);
				loadPlanXMLFile(profileName, cmscCatalog);
			} else {
				//LinkedList<Course> req = getAllMajorCourses("isCatalogTEST.xml");
				loadPlanXMLFile(profileName, "isCatalogTEST.xml");
			}
		}
	}
	
	/**
	 * Name: saveProfileXML
	 * Precondition:  None
	 * Postcondition: Saves the whole programs information to a xml file.
	 */
	public void saveProfileXML(){
		try {
			FileWriter fstream = new FileWriter(profileFile);
			BufferedWriter out = new BufferedWriter(fstream);
			
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			out.write(XMLCoder());
			out.close();
		 } catch (Exception e){
			 System.err.println("Error: " + e.getMessage());
		 }
	}
	
	/**
	 * Name: getAllPlanNames
	 * Precondition: None
	 * Postcondition: Gets all the plan names from the file.
	 * @return - An array of plan names.
	 */
	public String[] getAllPlanNames(){
		String plans = findXMLElement(profileFile, "/profile/plan/@*");
			
		if(!plans.equals("")){
			plans = plans.trim();
			String[] p = plans.split(" ");
			return p;
		} else {
			String[] none = {};
			return none;
		}
	}

	/**
	 * Name: loadPlanXMLFile
	 * Precondition: Needs a Plan name and a major catalog file.
	 * Postcondition: Loads information from file to Student and DegreeProgress classes.
	 * @param planName - Plan to be reading from file.
	 * @param catalogFile - Major catalog for loading information on a course if needed.
	 */
	public void loadPlanXMLFile(String planName, String catalogFile){
		if(fileExists(profileFile)){
			try {
				/********************   Gets Student Information *********************************************/
				String sName = findXMLElement(profileFile, "/profile/@*");
				String mCount = numXMLElement(profileFile, "count(/profile/plan/major)");
				int temp = Integer.parseInt(mCount);
				String major="";
				for(int x = 1; x <= temp; x ++)
					major = major + findXMLElement(profileFile, 
							"/profile/plan[@name='"+ planName +"']/major["+ x + "]");
			
				LinkedList<String> maj = null;
				if(!major.equals("")){
					major = major.trim();
					String[] m = major.split(" ");
					maj = new LinkedList<String>(Arrays.asList(m)); 
				}
				String minor = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']/minor");
				student = new Student(sName, maj, minor.trim());
				
				
				/********************   Gets Completed Courses ***********************************************/
				// Gets the Number of Completed Terms
				String cTCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']/completed)");
				int cTTemp = 0;
				if(!cTCount.equals(""))
					cTTemp = Integer.valueOf(cTCount.trim()).intValue();
				LinkedList<Course> lC = new LinkedList<Course>();
				
				// Gets the Completed Courses From the file.
				for(int x = 1; x <= cTTemp; x++){
					// Gets the Season & Year combo (Example: Fall2010)
					//String season = findXMLElement(file, "/profile/plan[@name='"+ profileName +"']/completed[" + x + "]/@*");
					
					//Gets how many courses in this Term
					String cCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']/completed[" + x + "]/course)");
					
					int cTemp = 0;
					if(!cCount.equals(""))
						cTemp = Integer.valueOf(cCount.trim()).intValue();
					
					Course pCourse = null;
					for(int y = 1; y <= cTemp; y++){
						String c = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/completed[" + x + "]/course/@*");
						c = c.trim();
						
						String courseID = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/completed[" + x + "]/course[" + y + "]/@*");
						courseID = courseID.trim();
						
						String grade = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/completed[" + x + "]/course[" + y + "]/grade");
						
						String notes = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/completed[" + x + "]/course[" + y + "]/notes");
						
						pCourse = findCourseInfoXML(catalogFile, courseID);
						if(pCourse != null){
							if(notes.length() != 0){
								pCourse.setGrade(grade.charAt(0));
								pCourse.setNotes(notes);
							} else 
								pCourse.setGrade(grade.charAt(0));
						} else 
							System.err.println("Error: Course not in Catalog.");

						lC.add(pCourse);
					}

					if(DEBUG)
						System.out.println("\n\n" + lC.toString()+ "\n\n");
				}
				
				
				/********************   Gets Planned Semesters ***********************************************/
				// Gets the Number of Planned Terms
				String pTCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']" +
												"/planned/@*)");
				int pTTemp = 0;
				if(!pTCount.equals(""))
					pTTemp = Integer.valueOf(pTCount.trim()).intValue();
				
				//System.err.println("Num of Planned: " + pTTemp);
				// Gets the planned courses
				LinkedList<Plan> lPlan = new LinkedList<Plan>();
				Plan p = null;
				List<Semester> lS = new LinkedList<Semester>();
				for(int x = 1; x <= pTTemp; x++){
					String season = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/planned[" + x + "]/@*");
					if(DEBUG)
						System.err.println("Season:" + season.trim() + "|");
					
					//Gets how many courses in this Term
					String cCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']" +
													"/planned[" + x + "]/course)");
					if(DEBUG)
						System.err.println("# of Course's: " + cCount);
					
					int cTemp = 0;
					if(!cCount.equals(""))
						cTemp = Integer.valueOf(cCount.trim()).intValue();
					
					
					String[] sea = season.split(" ");
					int year = Integer.parseInt(sea[1]);
					Semester s = new Semester(sea[0], year);
					for(int y = 1; y <= cTemp; y++){
						String courseNumber = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/planned[" + x + "]/course/@*");
						courseNumber = courseNumber.trim();

						String courseID = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
														"/planned[" + x + "]/course[" + y + "]/@*");
						courseID = courseID.trim();
						
						String grade = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
														"/planned[" + x + "]/course[" + y + "]/grade");

						String notes = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
														"/planned[" + x + "]/course[" + y + "]/notes");

						
						Course pCourse = findCourseInfoXML(catalogFile, courseID);
						if(pCourse != null){
							if(notes.length() != 0)
								pCourse.setNotes(notes);
								pCourse.setGrade(grade.charAt(0));
							s.addCourse(pCourse);
							//System.err.println(s.toString());
						} else { 
							System.err.println("Error: Course not in Catalog.");
						}
					}
					lS.add(s);
				}
				p = new Plan(lS, planName);
				lPlan.add(p);
				
				if(DEBUG)
					System.out.println(lPlan.toString());
				
				
				/********************   Gets Completed GEP's ***********************************************/
				// Gets the Number of Completed GEP's
				String cgepTCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']" +
												"/completedGEP/course)");
				int cgepTTemp = 0;
				if(!cgepTCount.equals(""))
					cgepTTemp = Integer.valueOf(cgepTCount.trim()).intValue();
				if(DEBUG)
					System.err.println("Num of CompletedGEP's: " + cgepTTemp);
				LinkedList<String> lCGEP =  new LinkedList<String>();
				for(int x = 1; x <= cgepTTemp; x++){
					String cgep = numXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
					"/completedGEP/course[" + x + "]");
					lCGEP.add(cgep);
				}
				
				if(DEBUG)
					System.out.println(lCGEP.toString());
				
				
				/********************   Gets Requirements    ***********************************************/
				// Gets the Number of Requirements
				String reqTCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']" +
												"/requirements/course)");
				int reqTTemp = 0;
				if(!reqTCount.equals(""))
					reqTTemp = Integer.valueOf(reqTCount.trim()).intValue();
				
				if(DEBUG)
					System.err.println("Num of Requirement's: " + reqTTemp);
				LinkedList<Course> lREQ =  new LinkedList<Course>();
				for(int x = 1; x <= reqTTemp; x++){
					Course pCourse = null;
					String c = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/requirements/course/@*");
					c = c.trim();
						
					String courseID = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/requirements/course[" + x + "]/@*");
					courseID = courseID.trim();
						
					String grade = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/requirements/course[" + x + "]/grade");
						
					String notes = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
													"/crequirements/course[" + x + "]/notes");
						
					pCourse = findCourseInfoXML(catalogFile, courseID);
					if(pCourse != null){
						if(notes.length() != 0){
							pCourse.setGrade(grade.charAt(0));
							pCourse.setNotes(notes);
						} else 
							pCourse.setGrade(grade.charAt(0));
					} else 
						System.err.println("Error: Course not in Catalog.");

					lREQ.add(pCourse);					
				}
				
				if(DEBUG)
					System.out.println("\n\n" + lREQ.toString()+ "\n\n");
				
				
				/********************   Gets GEP Requirements **********************************************/
				// Gets the Number of GEP Requirements
				String gepReqTCount = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']" +
												"/geprequirements/course)");
				int gepReqTTemp = 0;
				if(!gepReqTCount.equals(""))
					gepReqTTemp = Integer.valueOf(gepReqTCount.trim()).intValue();
				
				//System.err.println("Num of GEP Requirement's: " + gepReqTTemp);
				LinkedList<String> lGEPREQ =  new LinkedList<String>();
				for(int x = 1; x <= gepReqTTemp; x++){
					String req = numXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']" +
					"/geprequirements/course[" + x + "]");
					lGEPREQ.add(req);
				}
				
				if(DEBUG)
					System.out.println(lGEPREQ.toString());
				
				
				/********************   Gets Degree credits, GPA & Points **********************************/
				String degreeCredits = findXMLElement(profileFile, "/profile/plan/degreeCredits");
				int degCredits = Integer.parseInt(degreeCredits.trim());
				String degreegpa = findXMLElement(profileFile, "/profile/plan/degreeGPA");
				double degGPA = Double.parseDouble(degreegpa.trim());
				String degreePoints = findXMLElement(profileFile, "/profile/plan/degreePoints");
				double degPts = Double.parseDouble(degreePoints.trim());
				
				// TODO Add Degree Info(credits, gpa, points) to DegreeProgress
				progress = new DegreeProgress(lPlan, lC, lCGEP, lREQ, lGEPREQ, degCredits, degGPA, degPts);
				//System.out.println(progress.XMLCoder());
			} catch (XPathExpressionException e) {
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.err.println("ERROR: File not found.");
		}
	}
	
	public void setCurrentPlanName(String name){
		currentPlan = name;
	}
	
	public String getCurrentPlanName(){
		return currentPlan;
	}
	
	/**
	 * Name: findXMLElement
	 * Precondition: None
	 * Postcondition: 
	 * @param file
	 * @param item
	 * @return
	 */
	private String findXMLElement(String file, String item){
		try {
			DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
			domFactory.setNamespaceAware(true);
			DocumentBuilder builder;
		
			builder = domFactory.newDocumentBuilder();
		
			File f = new File(file);
		
			if(f.exists()){
				Document doc = builder.parse(file);

				XPathFactory factory = XPathFactory.newInstance();
				XPath xpath = factory.newXPath();
				XPathExpression expr = xpath.compile(item);

				Object result = expr.evaluate(doc, XPathConstants.NODESET);
				NodeList nodes = (NodeList) result;
	
				String temp = "";
				//System.out.println("All INFO: " + nodes.item(0).getTextContent()); // Get everything in course

				for (int i = 0; i < nodes.getLength(); i++)
					temp = temp + nodes.item(i).getTextContent() + " ";
	
				return temp;
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return "";
	}
	
	/**
	 * Name: numXMLElement
	 * Precondition: None
	 * Postcondition:
	 * @param file
	 * @param item
	 * @return
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 * @throws XPathExpressionException
	 */
	private String numXMLElement(String file, String item) throws ParserConfigurationException, 
							SAXException, IOException, XPathExpressionException {

		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true);
		DocumentBuilder builder = domFactory.newDocumentBuilder();
		File f = new File(file);
		if(f.exists()){
			Document doc = builder.parse(file);

			XPathFactory factory = XPathFactory.newInstance();
			XPath xpath = factory.newXPath();
			XPathExpression expr = xpath.compile(item);
			
			Object result = expr.evaluate(doc);
		
			return (String) result;
		}
	
		return null;
	}
	
	/**
	 * Name: getProgress
	 * Precondition: None
	 * Postcondition: returns the DegreeProgress instance
	 */
	public DegreeProgress getProgress(){ return progress; }
	
	/**
	 * Name: getStudent
	 * Precondition: None
	 * Postcondition: returns the Student instance
	 */
	public Student getStudent(){ return student; }
	
	/**
	 * Name: planNamer
	 * Precondition: Needs a student class
	 * Postcondition: Plan name 
	 * @return - The plan's name
	 */
	public String planNamer(){	
		String planName = "";
		String[] name = student.getFileMajor().split(" ");
		for(int i = 0; i < name.length; i++)
			planName += name[i] + "_";
		//planName = student.getFileMajor();
		if(!student.getMinor().equals(""))
			planName = planName + student.getMinor();
		//planName = planName + ".xml";
		if(fileExists(planName+ ".xml") == false){
			
			//System.out.println("NEW FILE CREATED: " + planName+ ".xml");
		} else {
			///System.out.println("FILE Exists");
			String temp = planName;
			int count = 1;
			while(fileExists(temp)){
				temp = planName;
				temp = temp + "_" + count; 
				System.out.println("temp");
				count++;
			}
				
		}
		currentPlan = planName;
		return planName;
	}
	
	/**
	 * Name: fileExists
	 * Precondition:  None
	 * Postcondition: Determines if file exists or not
	 * @param file - File to be checked to see if it already exists or not.
	 * @return true  - File exists
	 *         false - File doesn't exist
	 */
	public boolean fileExists(String file){
		File f = new File(file);
		
		// Checks to see if file exists or not.
		if (f.exists()) 
			return true;
		    
		return false;
	}
	
	/**
	 * Name: XMLCoder
	 * Precondition:  None
	 * Postcondition: Gathers information on class to be displayed 
	 *                in XML format for saving to file.
	 * @return - Output on the class to be displayed in XML format
	 */
	public String XMLCoder(){
		String xml = "";
		
		xml = "<profile name=\"" + student.getName().trim()  + "\">\n";
		xml = xml + " <plan name=\"" + planNamer() + "\">\n";
		xml = xml + student.XMLCoder();
		xml = xml + progress.XMLCoder();
		xml = xml + "</plan>\n";
		xml = xml + "</profile>\n";
		
		return xml;
	}
	
	/**
	 * Name: toString()
	 * Precondition:  None
	 * Postcondition: Gathers information on the class to be displayed
	 * @return - Output on the class to be displayed
	 */
	public String toString(){
		
		String text = student.toString();
		//text = text + plan.toString();
		if(progress != null)
			text = text + progress.toString();
		
		return text;
		//return progress.toString();
	}

	////////////////////////////////////////////////////////////////////
	
	/**
	 * Name: getAllCourses
	 * Precondition: File to be getting all the courses.
	 * Postcondition: List of Courses
	 * @param file
	 * @return - LinkedList of courses
	 */
	public LinkedList<Course> getAllMajorCourses(String file){
		int num = getNumberOfCourses(file, "count(/catalog/course/@*)");
		
		if(num != 0){
			LinkedList<Course> c = new LinkedList<Course>();
			for(int x = 1; x <= num; x ++){
				Course temp = findCourseInfoXML(file, x);
				c.add(temp);
			}
			return c;
		}
		
		return null;
	}
	
	/**
	 * Name: getAllGEPCourses
	 * Precondition: File to be getting all the gep course names.
	 * Postcondition: List of course names
	 * @param file
	 * @return - LinkedList of gep courses names
	 */
	public LinkedList<String> getAllGEPCourses(String file){
		int num = getNumberOfCourses(file, "count(/catalog/course/@*)");
		if(num != 0){
			LinkedList<String> c = new LinkedList<String>();
			for(int x = 1; x <= num; x ++){
				Course temp = findCourseInfoXML(file, x);
				c.add(temp.getName());
			}
			return c;
		}
		
		return null;
	}
	
	/**
	 * Name: getNumberOfCourses
	 * Precondition: Needs a file and a path for courses like "count(/catalog/course/@*)"
	 * Postcondition: Number of Courses in the file.
	 * @param file - File to be getting the number of Courses from.
	 * @param item - Path to find the number of courses.
	 * @return - int - the number of courses in the file.
	 */
	private int getNumberOfCourses(String file, String item) {
		try {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setNamespaceAware(true);
		DocumentBuilder builder;
		
			builder = domFactory.newDocumentBuilder();
		
		File f = new File(file);
		if(f.exists()){
			Document doc = builder.parse(file);

			XPathFactory factory = XPathFactory.newInstance();
			XPath xpath = factory.newXPath();
			XPathExpression expr = xpath.compile(item);
			
			Object result = expr.evaluate(doc);
			Integer in = new Integer(result.toString());
			return in;
		}
	
		
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * Name: findCourseInfoXML
	 * Description: Finds the courseId in the file and gets the courses information.
	 * @param file - File to be searched.
	 * @param courseId - Course Id to be found in the file.
	 */
	public Course findCourseInfoXML(String file, int x){
		//title, dept, number, credits, prereq, or description
		
		// Gets Course Name
		String name = findXMLElement(file, "/catalog/course["+ x +"]/@*");
		
		// Gets Course Title
		String title = findXMLElement(file, "/catalog/course["+ x +"]/title/text()");//@id='"+ courseId +"']/title/text()");

		// Gets Course Dept
		String dept = findXMLElement(file, "/catalog/course["+ x +"]/dept/text()");
		
		// Gets Course Number
		String number = findXMLElement(file, "/catalog/course["+ x +"]/num/text()");
		int num=0;
		if(!number.equals(""))
			num = Integer.valueOf(number.trim()).intValue();
		
		// Gets Course Credits
		String credits = findXMLElement(file, "/catalog/course["+ x +"]/credits/text()");
		int cred=0;
		if(!credits.equals(""))
			cred = Integer.valueOf(credits.trim()).intValue();
		
		// Gets Course PreReq
		String prereq = findXMLElement(file, "/catalog/course["+ x +"]/prereq/text()");
		LinkedList<String> pre = null;
		if(!prereq.equals("")){
			prereq = prereq.trim();
			String[] p = prereq.split(" ");
			pre = new LinkedList<String>(Arrays.asList(p)); 
		}
		
		// Gets Course GEP
		String gep = findXMLElement(file, "/catalog/course["+ x +"]/gepreq/text()");
		LinkedList<String> GEP = null;
		if(!gep.equals("")){
			gep = gep.trim();
			String[] g = gep.split(" ");
			GEP = new LinkedList<String>(Arrays.asList(g));
		}
		// Gets Course Description
		String description = findXMLElement(file, "/catalog/course["+ x +"]/desc/text()");
		
		if(!title.equals("")){
			Course c = new Course(name.trim(), dept.trim(), num, title.trim(), 
					description.trim(), ' ', cred , GEP, false, pre);
			//System.out.println(c.XMLCoder());
			return c;
			
		}
		return null;
	}
	
	/**
	 * Name: findCourseInfoXML
	 * Description: Finds the courseId in the file and gets the courses information.
	 * @param file - File to be searched.
	 * @param courseId - Course Id to be found in the file.
	 */
	public Course findCourseInfoXML(String file, String x){
		//title, dept, number, credits, prereq, or description
		
		// Gets Course Name
		String name = findXMLElement(file, "/catalog/course[@id='"+ x +"']/@*");
		
		// Gets Course Title
		String title = findXMLElement(file, "/catalog/course[@id='"+ x +"']/title/text()");//@id='"+ courseId +"']/title/text()");

		// Gets Course Dept
		String dept = findXMLElement(file, "/catalog/course[@id='"+ x +"']/dept/text()");
		
		// Gets Course Number
		String number = findXMLElement(file, "/catalog/course[@id='"+ x +"']/num/text()");
		int num=0;
		if(!number.equals(""))
			num = Integer.valueOf(number.trim()).intValue();
		
		// Gets Course Credits
		String credits = findXMLElement(file, "/catalog/course[@id='"+ x +"']/credits/text()");
		int cred=0;
		if(!credits.equals(""))
			cred = Integer.valueOf(credits.trim()).intValue();
		
		// Gets Course PreReq
		String prereq = findXMLElement(file, "/catalog/course[@id='"+ x +"']/prereq/text()");
		LinkedList<String> pre = null;
		if(!prereq.equals("")){
			prereq = prereq.trim();
			String[] p = prereq.split(" ");
			pre = new LinkedList<String>(Arrays.asList(p)); 
		}
		
		// Gets Course GEP
		String gep = findXMLElement(file, "/catalog/course[@id='"+ x +"']/gepreq/text()");
		LinkedList<String> GEP = null;
		if(!gep.equals("")){
			gep = gep.trim();
			String[] g = gep.split(" ");
			GEP = new LinkedList<String>(Arrays.asList(g));
		}
		// Gets Course Description
		String description = findXMLElement(file, "/catalog/course[@id='"+ x +"']/desc/text()");
		
		if(!title.equals("")){
			Course c = new Course(name.trim(), dept.trim(), num, title.trim(), 
					description.trim(), ' ', cred , pre, false, GEP);
			//System.out.println(c.XMLCoder());
			return c;
			
		}
		return null;
	}	
	
	public String[] getPlannedSemesters(String planName){
		String num;
		try {
			num = numXMLElement(profileFile, "count(/profile/plan[@name='"+ planName +"']/planned/@*)");
		
			int numTTemp = 0;
			if(!num.equals(""))
				numTTemp = Integer.valueOf(num.trim()).intValue();
		
			String[] planned = new String[numTTemp];
			for(int x = 1; x <= numTTemp; x ++){
				String plans = findXMLElement(profileFile, "/profile/plan[@name='"+ planName +"']/planned[" + x + "]/@*");
				planned[x-1] = plans;
			}
			
			return planned;
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] none = {};
		return none;
	}
}
